<template>
  <div>
    <div v-loading="loading">
      <census-complex
        v-if="!isSimpleCensus && hasCustomerRelated"
        :view-mode="viewMode"
        :related-id="customerRelatedId"
      ></census-complex>
      <census-simple
        v-else-if="hasCustomerRelated"
        :view-mode="viewMode"
        :related-id="customerRelatedId"
      ></census-simple>
    </div>
  </div>
</template>
<script>
import CensusComplex from '@/credit/views/city/task/Census'
import CensusSimple from '@/credit/views/city/task/CensusSimple'

const CREDIT_SERVER = 'credit'

export default {
  components: {
    CensusComplex,
    CensusSimple,
  },
  props: {
    viewMode: Boolean,
    relatedId: String,
    isSimple: Boolean,
  },
  data() {
    return {
      loading: false,
      customerRelatedId: '',
      customerData: {},
      hasCustomerRelated: false,
      isSimpleCensusReport: true,
    }
  },
  computed: {
    isSimpleCensus() {
      return this.isSimple || this.isSimpleCensusReport
    },
  },
  watch: {
    relatedId() {
      this.getData()
    },
  },
  created() {
    this.getData()
  },
  mounted() {},
  methods: {
    async getData() {
      this.customerRelatedId = this.relatedId || this.$route.query.id
      this.getRelatedView()
    },

    // 关联基本信息
    async getRelatedView() {
      // eslint-disable-next-line
      return new Promise(async (resolve, reject) => {
        this.loading = true
        const [res, err] = await this.$utils.promise(
          this.$api.getData(CREDIT_SERVER, 'customer_related_base_view', {
            'customer_related_id.eq': this.customerRelatedId,
          })
        )
        this.loading = false
        if (res) {
          this.hasCustomerRelated = true
          this.customerData = (res.data && res.data.length && res.data[0]) || {}
          this.isSimpleCensusReport = this.customerData.is_simple_process === '1'
          resolve()
        } else {
          reject(err)
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped></style>
